﻿
using commonImportPlugin.Common;
using commonImportPlugin.Config;
using commonImportPlugin.Configs.MedicalFile;
using commonImportPlugin.Factorys;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace commonImportPlugin.FilesCore.MedicalFile
{
    /// <summary>
    /// 《普通模式通用医保数据解析核心类》
    /// 该模式支持医保文件内容以行为单位，行内
    /// 数据项以特定分隔符分割。该模式支持绝大
    /// 部分医保数据
    /// </summary>
    public class MedicalFileCoreClass : MedicalFileCore
    {
        public MedicalFileBase medicalBase = new MedicalFileBase();
        static MedicalFileConfig config = null;
        static Context context;
        public MedicalFileCoreClass GetInstance()
        {
            if (IsGetConfigSucceed())
            {
                context = Context.GetConfig();
                return new MedicalFileCoreClass();
            }
            return null;
        }
        private bool IsGetConfigSucceed()
        {
            config = MedicalFileConfig.GetConfig();
            if (config == null)
                return false;
            else
                return true;
        }
        /// <summary>
        /// 获取所有的医保数据
        /// </summary>
        /// <param name="medicalFiles">医保数据文件集</param>
        /// <param name="medicalCounts">成功获取的医保数据量</param>
        /// <returns>医保数据</returns>
        public override List<MedicalData> GetMedicalAllDatas(List<string> medicalFiles, out int medicalCounts)
        {
            List<MedicalData> allMedicalDatas = new List<MedicalData>();
            foreach (string medicalFile in medicalFiles)
            {
                medicalBase.getFileMedicalDatas(allMedicalDatas, medicalFile, config);
            }
            medicalCounts = allMedicalDatas.Count;
            return allMedicalDatas;       
        }

        /// <summary>
        /// 获取医保数据量
        /// </summary>
        /// <param name="MedicalFiles">医保数据文件集合</param>
        /// <returns>医保数据量</returns>
        public override int GetMedicalAllCounts(List<string> MedicalFiles)
        {
            int allMedicalCount = 0;
            foreach (string medicalFile in MedicalFiles)
            {
                string[] originTrackLines = null;
                if (!File.Exists(medicalFile))
                    throw new Exception("医保文件不存在！");
                originTrackLines = File.ReadAllLines(medicalFile,ASCIIEncoding.GetEncoding(config.encoding));
                int eachFileCount = originTrackLines.Length - config.dataIndex + 1;
                allMedicalCount += eachFileCount;
            }
            return allMedicalCount;
        }

        public override bool CheckMedicalData(string Data, string regex)
        {
            throw new NotImplementedException();
        }
    }
}
